package uk.co.q3c.deplan.client;

import java.util.logging.Logger;

import uk.co.q3c.deplan.client.dao.Cache;
import uk.co.q3c.deplan.client.domain.DomainObject;
import uk.co.q3c.deplan.client.service.ServiceFactory;
import uk.co.q3c.deplan.client.ui.view.ScreenView;
import uk.co.q3c.deplan.client.ui.view.ViewCreator;
import uk.co.q3c.deplan.client.ui.view.ViewManager;
import uk.co.q3c.deplan.client.ui.view.Viewtags;

/**
 * Holds a {@link ServiceFactory} instance injected by either client or server side start up code. This allows the {@link ServiceFactory} to
 * be client, server or test related, while giving a common point of access for domain objects
 * 
 * @author David Sowerby 5 May 2010
 * 
 */
public class Orchestrator {
	protected static final Logger logger = Logger.getLogger(Orchestrator.class.getName());
	private static Cache cache;
	private static ServiceFactory serviceFactory;
	private static ViewCreator viewCreator;
	private static ViewManager viewManager = new ViewManager();
	private static ScreenView screenView;

	public static void setServiceFactory(ServiceFactory factory) {
		serviceFactory = factory;

	}

	public static ServiceFactory serviceFactory() {

		if (serviceFactory == null) {
			logger.severe("Service factory has not been set");
		}
		return serviceFactory;
	}

	public static ViewManager viewManager() {
		if (viewManager == null) {
			logger.severe("View manager has not been set");
		}
		return viewManager;
	}

	public static void setScreenView(ScreenView screenView) {
		Orchestrator.screenView = screenView;
	}

	public static ScreenView screenView() {
		if (screenView == null) {
			logger.severe("Screen View has not been set");
		}
		return screenView;
	}

	public static void setViewCreator(ViewCreator viewCreator) {
		Orchestrator.viewCreator = viewCreator;
		viewManager.setViewCreator(Orchestrator.viewCreator);
	}

	public static Viewtags currentViewTag() {
		return Orchestrator.screenView.getPresenter()
				.getCurrentCoreView()
				.viewTag();
	}

	public static DomainObject objectInView() {
		return Orchestrator.screenView()
				.getPresenter()
				.getCurrentCoreView()
				.getPresenter()
				.getObjectInView();
	}

	public static void setObjectInView(DomainObject dob) {
		Orchestrator.screenView()
				.getPresenter()
				.setObjectInView(dob);
	}

	public static Cache getCache() {
		if (cache == null) {
			cache = new Cache();
		}
		return cache;
	}
}
